#include "DFSSolver.h"


DFSSolver::DFSSolver(Node *root) : Solver(root)
{
}

void DFSSolver::solve(std::string order, std::string id_heuristic) {
	firstPush();
	std::string *order2;
	if (order.at(0) == 'R'){
		order2 = new std::string("LPGD");
	}
	else {
		order2 = new std::string(order);
	}
	
	while (!this->checkWinState()) {
		if (order.at(0) == 'R'){
			this->randomOrder(order2);
		}
		this->getNode();
		if (this->checkVisited()){
			continue;
		}
		this->createChildren(order2);
		this->pushChildren();
		countNode++;
		if (maxDepth < node->getLevel()){
			maxDepth = node->getLevel();
		}
	}
	if (node != NULL){
		std::string steps = findPath(node);
		std::cout << steps.size() << std::endl << steps;
	}
}

void DFSSolver::firstPush(){
	stack.push(node);
}

void DFSSolver::getNode(){
	node = stack.top();
	stack.pop();
}

bool DFSSolver::checkVisited(){
	bool exist = false;
	
	for (int n = 0; n < visited.size(); n++) {
		if (compare(visited.at(n), node)) {
			exist = true;
			break;
		}
	}
	if (!exist)
		visited.push_back(node);

	return exist;
}

void DFSSolver::createChildren(std::string *order){
	for (int j = 0; j < order->size(); j++) {
		node->createChild(order->at(j));
	}
}

void DFSSolver::pushChildren(){
	for (int i = node->getChildren()->size() - 1; i >= 0; i--) {
		stack.push(node->getChildren()->at(i));
		countChildren++;
	}
}

DFSSolver::~DFSSolver()
{
}
